package com.luanye.enjoy.service.impl;

import com.luanye.enjoy.service.TxSmsService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("txSmsService")
public class TxSmsServiceImpl implements TxSmsService {

    @Value("${tencent.SecretId}")
    private String SecretId;

    @Value("${tencent.SecretKey}")
    private String SecretKey;

    @Override
    public boolean sendSmsCode(String phone, String[] params, String templateId) {


        try {
            Credential cred = new Credential(SecretId, SecretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);

            SendSmsRequest req = new SendSmsRequest();

            String[] phoneNumberSet1 = {phone};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId("1400724569");
            req.setSignName("鸾烨网络");
            req.setTemplateId(templateId);

            req.setTemplateParamSet(params);

            SendSmsResponse resp = client.SendSms(req);
            if (resp.getSendStatusSet().equals("ok")) {
                return true;
            }
        } catch (TencentCloudSDKException e) {
            return false;
        }
        return true;
    }
}


